package com.demo.config;

import com.demo.service.CustomBot;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.updatesreceivers.ExponentialBackOff;

@Data
@Configuration
@ConfigurationProperties(prefix = "proxy")
public class BotOptions {

    private boolean enabled;
    private DefaultBotOptions.ProxyType type;
    private String host;
    private int port;

    private static final Logger logger = LoggerFactory.getLogger(CustomBot.class);

    @Bean
    public DefaultBotOptions defaultBotOptions() {
        logger.info("init bot options");
//        DefaultBotOptions defaultBotOptions = new DefaultBotOptions();
//        defaultBotOptions.setProxyHost("127.0.0.1");
//        defaultBotOptions.setProxyPort(7890);
//        // Select proxy type: [HTTP|SOCKS4|SOCKS5] (default: NO_PROXY)
//        defaultBotOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);

        DefaultBotOptions defaultBotOptions = new DefaultBotOptions();
        if(enabled){
            defaultBotOptions = new DefaultBotOptions();
            defaultBotOptions.setProxyHost(host);
            defaultBotOptions.setProxyPort(port);
            // Select proxy type: [HTTP|SOCKS4|SOCKS5] (default: NO_PROXY)
            defaultBotOptions.setProxyType(type);
        }else{
            defaultBotOptions.setBackOff(telegramExponentialBackOff());
        }
        return defaultBotOptions;
    }

    private ExponentialBackOff telegramExponentialBackOff() {
        return new ExponentialBackOff.Builder().build();
    }

}
